package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.dfs.exception.DfsException;
import la.iok.hzsvn.share.dfs.model.DfsDescriptor;
import la.iok.hzsvn.share.dfs.model.DfsResult;
import la.iok.hzsvn.share.dfs.service.DfsService;
import la.iok.hzsvn.share.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import la.iok.hzsvn.lewin.movie.service.UserFileService;

@Service("webDfsService")
public class WebDfsServiceImpl implements WebDfsService {
    private final DfsService dfsService;

    public WebDfsServiceImpl(DfsService dfsService) {
        this.dfsService = dfsService;
    }

    @Override
    public void downloadFile(@NotNull HttpServletResponse response, @NotNull DfsDescriptor descriptor, @Nullable String fileName){
        try(OutputStream out = response.getOutputStream()) {
            response.setContentType("application/x-download;charset=UTF-8");
            if(StringUtils.isNotBlank(fileName)){
                String filename = URLEncoder.encode(fileName, "UTF-8");
                response.addHeader("Content-Disposition", "attachment;filename=" + filename);
            }
            response.setContentLengthLong(getFileLength(descriptor));
            DfsResult res = dfsService.downloadFile(out,descriptor);
            out.flush();
            if(!res.success()){
                throw new DfsException(res.getMessage());
            }
        } catch (IOException e) {
            throw new DfsException("下载文件失败",e);
        }
    }

    @Override
    public DfsResult uploadFile(String filePath) throws DfsException {
        return dfsService.uploadFile(filePath);
    }

    @Override
    public DfsResult uploadFile(File file) throws DfsException {
        return dfsService.uploadFile(file);
    }

    @Override
    public DfsResult uploadFile(String storePath, String filePath) throws DfsException {
        return dfsService.uploadFile(storePath,filePath);
    }

    @Override
    public DfsResult uploadFile(String storePath, File file) throws DfsException {
        return dfsService.uploadFile(storePath,file);
    }

    @Override
    public DfsResult downloadFile(OutputStream outputStream, DfsDescriptor descriptor) throws DfsException {
        return dfsService.downloadFile(outputStream,descriptor);
    }

    @Override
    public DfsResult deleteFile(DfsDescriptor descriptor) throws DfsException {
        return dfsService.deleteFile(descriptor);
    }

    @Override
    public DfsDescriptor dfsFile(String path) {
        return dfsService.dfsFile(path);
    }

    @Override
    public String storePath(String digest, File file) {
        return dfsService.storePath(digest,file);
    }

    @Override
    public String storePath(String digest, String fileName) {
        return dfsService.storePath(digest,fileName);
    }

    @Override
    public long getFileLength(DfsDescriptor descriptor) {
        return dfsService.getFileLength(descriptor);
    }

    @Override
    public boolean exist(DfsDescriptor descriptor) {
        return dfsService.exist(descriptor);
    }
}
